package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.TbTest;
import com.ruoyi.system.domain.WaveFile;
import com.ruoyi.system.service.ITbTestService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: zhoupeng
 * @date: 2021/9/14 23:17
 * @descreption: 测试数据控制层
 */
@Slf4j
@Controller
@RequestMapping("/system/tbTest")
public class TbTestController extends BaseController {

    private String prefix = "system/tbTest";

    @Value("${file.download.dir}")
    private String realPath;

    @Resource
    private ITbTestService tbTestService;

    @RequiresPermissions("system:tbTest:view")
    @GetMapping()
    public String tbTest(ModelMap mmap) {
        List<String> deviceNameList = tbTestService.selectDeviceNameList();
        List<String> devicePnList = tbTestService.selectDevicePnList();
        List<String> deviceSnList = tbTestService.selectDeviceSnList();
        List<String> testSubItemNoList = tbTestService.selectTestSubItemNoList();
        mmap.put("deviceNameList", deviceNameList);
        mmap.put("devicePnList", devicePnList);
        mmap.put("deviceSnList", deviceSnList);
        mmap.put("testSubItemNoList", testSubItemNoList);
        return "system/tbTest/tbTest";
    }

    /**
     * 查询列表
     */
    @RequiresPermissions("system:tbTest:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TbTest tbTest) {
        startPage();
        List<TbTest> list = tbTestService.selectTbTestList(tbTest);
        return getDataTable(list);
    }

    /**
     * 导出列表
     */
    @RequiresPermissions("system:tbTest:export")
    @Log(title = "导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TbTest tbTest) {
        List<TbTest> list = tbTestService.selectTbTestList(tbTest);
        ExcelUtil<TbTest> util = new ExcelUtil<>(TbTest.class);
        return util.exportExcel(list, "导出数据");
    }

    /**
     * 新增
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存
     */
    @RequiresPermissions("system:tbTest:add")
    @Log(title = "新增", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TbTest tbTest) {
        return toAjax(tbTestService.insertTbTest(tbTest));
    }

    /**
     * 修改
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        TbTest tbTest = tbTestService.selectTbTestById(id);
        mmap.put("tbTest", tbTest);
        return "system/tbTest/edit";
    }

    /**
     * 修改保存
     */
    @RequiresPermissions("system:tbTest:edit")
    @Log(title = "修改", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TbTest tbTest) {
        return toAjax(tbTestService.updateTbTest(tbTest));
    }

    /**
     * 删除
     */
    @RequiresPermissions("system:tbTest:remove")
    @Log(title = "删除", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(tbTestService.deleteTbTestByIds(ids));
    }

    /**
     * 波形文件页面
     */
    @GetMapping("/viewWave")
    public String viewWave(String fileName, ModelMap mmap) throws Exception {
        mmap.put("fileName", fileName);
        return "system/tbTest/viewWave";
    }

    /**
     * 查看波形文件图表
     */
    @PostMapping("/waveData")
    @ResponseBody
    public TableDataInfo viewWaveChart(String fileName, String startTime, String internal) throws Exception {
        if (startTime.isEmpty()) {
            startTime = "0";
        }
        if (internal.isEmpty()) {
            internal = "60";
        }
        List<WaveFile> list = new ArrayList<>();
        Reader reader = new FileReader(realPath + fileName);
        long time = System.currentTimeMillis();
        Iterable<CSVRecord> records = CSVFormat.RFC4180.withHeader("time", "plungerPumpValue", "centrifugalPumpValue").parse(reader);
        for (CSVRecord csvRecord : records) {
            if (csvRecord.get("plungerPumpValue").contains("PG10")) {
                continue;
            }
            WaveFile waveFile = new WaveFile();
            waveFile.setTime(csvRecord.get("time"));
            waveFile.setPlungerPumpValue(csvRecord.get("plungerPumpValue"));
            waveFile.setCentrifugalPumpValue(csvRecord.get("centrifugalPumpValue"));
            if (Double.valueOf(waveFile.getTime()) >= Double.valueOf(startTime) &&
                    Double.valueOf(waveFile.getTime()) <= Double.valueOf(startTime) + Double.valueOf(internal)) {
                list.add(waveFile);
            }
        }
        reader.close();
        time = System.currentTimeMillis() - time;
        log.info("解析波形文件耗时: " + time + "毫秒");
        return getDataTable(list);
    }

    /**
     * 详情查看
     */
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Integer id, ModelMap mmap) {
        TbTest tbTest = tbTestService.selectTbTestById(id);
        mmap.put("tbTest", tbTest);
        return "system/tbTest/detail";
    }


    /**
     * 查看曲线图
     */
    @GetMapping("/viewDiagram")
    public String viewDiagram(String fileName, ModelMap mmap) throws Exception {
        mmap.put("imgUrl", "/getDiagram/"+fileName);
        return "system/tbTest/diagram";
    }

}
